package com.lollipop.rabbitmqspringboot.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Lollipop
 * @Date: 2021-10-08 20:56
 * @Description:
 */
@Configuration
public class TTLQueueConfig {
    /**
     * 普通交换机名称
     */
    public static final String X_EXCHANGE = "X";
    /**
     * 死信交换机名称
     */
    public static final String X_DEAD_EXCHANGE = "DX";
    /**
     * 普通队列A
     */
    public static final String QUEUE_A = "QA";
    /**
     * 普通队列B
     */
    public static final String QUEUE_B = "QB";
    /**
     * 普通队列C
     */
    public static final String QUEUE_C = "QC";
    /**
     * 死信队列
     */
    public static final String DEAD_QUEUE = "QD";

    /**
     * 声明普通交换机
     *
     * @return
     */
    @Bean
    public DirectExchange xExcahnge() {
        return new DirectExchange(X_EXCHANGE);
    }

    /**
     * 声明死信交换机
     *
     * @return
     */
    @Bean
    public DirectExchange dxExcahnge() {
        return new DirectExchange(X_DEAD_EXCHANGE);
    }

    /**
     * 声明延迟10s的队列A
     *
     * @return
     */
    @Bean
    public Queue queueA() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", X_DEAD_EXCHANGE);
        args.put("x-dead-letter-routing-key", "XD");
        // 设置TTL时间10s
        args.put("x-message-ttl", 10000);
        return QueueBuilder.durable(QUEUE_A).withArguments(args).build();
    }

    /**
     * 声明延迟40s的队列B
     *
     * @return
     */
    @Bean
    public Queue queueB() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", X_DEAD_EXCHANGE);
        args.put("x-dead-letter-routing-key", "XD");
        // 设置TTL时间40s
        args.put("x-message-ttl", 40000);
        return QueueBuilder.durable(QUEUE_B).withArguments(args).build();
    }

    /**
     * 声明无延迟的队列C
     *
     * @return
     */
    @Bean
    public Queue queueC() {
        Map<String, Object> args = new HashMap<>();
        // 设置死信交换机
        args.put("x-dead-letter-exchange", X_DEAD_EXCHANGE);
        // 设置死信routing-key
        args.put("x-dead-letter-routing-key", "XD");
        return QueueBuilder.durable(QUEUE_C).withArguments(args).build();
    }

    /**
     * 声明死信队列D
     *
     * @return
     */
    @Bean
    public Queue queueD() {
        return QueueBuilder.durable(DEAD_QUEUE).build();
    }

    /**
     * 队列A绑定交换机X
     *
     * @param queueA
     * @param xExcahnge
     * @return
     */
    @Bean
    public Binding queueABingdX(Queue queueA, DirectExchange xExcahnge) {
        return BindingBuilder.bind(queueA).to(xExcahnge).with("XA");
    }

    /**
     * 队列B绑定交换机X
     *
     * @param queueB
     * @param xExcahnge
     * @return
     */
    @Bean
    public Binding queueBBingdX(Queue queueB, DirectExchange xExcahnge) {
        return BindingBuilder.bind(queueB).to(xExcahnge).with("XB");
    }

    /**
     * 队列C绑定交换机X 没有TTL时间
     *
     * @param queueC
     * @param xExcahnge
     * @return
     */
    @Bean
    public Binding queueCBingdX(Queue queueC, DirectExchange xExcahnge) {
        return BindingBuilder.bind(queueC).to(xExcahnge).with("XC");
    }

    /**
     * 队列D绑定交换机DX
     *
     * @param queueD
     * @param dxExcahnge
     * @return
     */
    @Bean
    public Binding queueDBingdDx(Queue queueD, DirectExchange dxExcahnge) {
        return BindingBuilder.bind(queueD).to(dxExcahnge).with("XD");
    }
}
